package com.myecl.poker.util;

import java.util.LinkedList;
import java.util.List;
import java.util.TreeSet;

import com.myecl.poker.rules.Card;
import com.myecl.poker.rules.enums.Suit;


public class Sort {
	private Sort() {
	}

	public static LinkedList<TreeSet<Card>> bySuit(List<Card> cards) {
		LinkedList<TreeSet<Card>> sorted = new LinkedList<TreeSet<Card>>();
		for(Suit s: Suit.values()) {
			sorted.add(new TreeSet<Card>());
		}
		for(Card c: cards) {
			switch(c.getSuit()) {
				case DIAMONDS: sorted.get(0).add(c) ; break;
				case CLUBS: sorted.get(1).add(c); break;
				case HEARTS: sorted.get(2).add(c); break;
				case SPADES: sorted.get(3).add(c); break;
			}
		}
		return sorted;
	}
	
	public static TreeSet<Card> byRank(List<Card> cards) {
		return new TreeSet<Card>(cards);
	}
	
	public static LinkedList<TreeSet<Card>> sameRank(List<Card> cards) {
		LinkedList<TreeSet<Card>> sorted = new LinkedList<TreeSet<Card>>();
		TreeSet<Card> cardsByRank = byRank(cards);
		sorted.add(new TreeSet<Card>());
		sorted.getLast().add(cardsByRank.pollFirst());
		
		while(!cardsByRank.isEmpty()) {
			if(sorted.getLast().first().getRank().equals(cardsByRank.first().getRank())) {
				sorted.getLast().add(cardsByRank.pollFirst());
			} else {
				sorted.add(new TreeSet<Card>());
				sorted.getLast().add(cardsByRank.pollFirst());
			}
		}
		
		
		return sorted;
	}
	

}
